<?php

namespace app\index\model;

use app\model\Model;

class Detail extends Model
{
  public $title, $keywords, $description;
  public $c_main;
  public $db;

  function __construct()
  {
    $this->db = $GLOBALS['db'];
  }

  function index()
  {
    $id = intval($_GET['id']);
    non_numeric_href($id, $GLOBALS['lang']['illegal'], './');

    include_once LIB_PATH . 'cls.detail.php';
    include_once LIB_PATH . 'cls.channel.php';
    $objDetail = new \Detail();
    $detail = $objDetail->getDetail($id);

    if (empty($detail)) {
      alert_href($GLOBALS['lang']['illegal'], './');
    }
    $detail = stripslashes_deep($detail);
    // set_url_back();

    $objChannel = new \Channel();
    $channel = $objChannel->getChannel($detail['d_parent']);
    $channel['c_sub'] = $channel['c_ifsub'] ? $channel['c_sub'] : $channel['id'];

    $this->title = $detail['d_name'] . '-' . $channel['c_name'] . '-' . $GLOBALS['cms']['name'];
    $this->keywords = !empty($detail['d_keywords']) ? $detail['d_keywords'] : $detail['d_name'];
    $this->description = !empty($detail['d_description']) ? str_cut(str_text($detail['d_description'], 1), 220) : str_cut(str_text($GLOBALS['cms']['description'], 1), 220);

    //获取上级频道字段
    $channel_parent = $objChannel->getParent($channel['id']);

    // 获取频道相关
    $this->c_main = $channel['c_main'];
    $channel_slist = channel_slist($channel['c_ifsub'] == 1 ? $channel['id'] : $channel['c_parent'], $channel['id']);
    $current_channel_location = current_channel_location($channel['id'], $channel['id']);

    //获取顶级频道字段
    $channel_main = $objChannel->getMain($channel['id']);

    // prev&next
    $prev = $objDetail->getPrev($detail['id']);
    $next = $objDetail->getNext($detail['id']);

    // $objDetail->addCount($detail['id']); // 浏览自增

    // 默认评论插件调用
    // hook('changyan');

    $this->tpl($channel['c_dmodel'], [
      'db' => $this->db,
      'detail' => $detail,
      'channel' => $channel,
      'channel_parent' => $channel_parent,
      'channel_slist' => $channel_slist,
      'current_channel_location' => $current_channel_location,
      'channel_main' => $channel_main,
      'prev' => $prev,
      'next' => $next,
    ]);
  }

  function __destruct()
  {
  }
}
